package controller

import (
	"EnjoyWorking/model"
	"EnjoyWorking/utils"
	"EnjoyWorking/web/service"
	"github.com/kataras/golog"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/hero"
	"strings"
)

func NewJobController(app *iris.Application, party iris.Party) {
	var (
		user       = party.Party("/job")
		controller = Job{service: service.Job{}}
	)

	user.Post("/create", hero.Handler(controller.create)).Name = "创建岗位"
	user.Get("/getAll", hero.Handler(controller.getAll)).Name = "获取所有岗位"
	user.Get("/get", hero.Handler(controller.get)).Name = "获取单个岗位"
	user.Put("/update", hero.Handler(controller.update)).Name = "编辑岗位"
	user.Delete("/delete", hero.Handler(controller.delete)).Name = "删除岗位"
	user.Get("/getByDepartmentId", hero.Handler(controller.getByDepartmentId)).Name = "根据部门ID获取岗位"

	list := app.GetRoutes()
	for _, v := range list {
		if v.Method == "OPTIONS" {
			continue
		}
		if strings.Index(v.Path, "/api/job") != -1 {
			v.Description = "岗位管理接口"
		}
	}
}

type Job struct {
	service service.Job
}

func (c *Job) getByDepartmentId(ctx iris.Context) {
	var (
		code       utils.Code
		err        error
		uid        int64
		total      int64
		result     []*model.Job
		resultCopy []*model.JobCopy
	)

	if uid, err = ctx.URLParamInt64("id"); err != nil {
		goto ERR
	}

	if total, result, err = c.service.FindByDepartmentId(uint(uid)); err != nil {
		goto ERR
	}

	for _, v := range result {
		resultCopy = append(resultCopy, &model.JobCopy{*v})
	}

	utils.PaginationTableData(ctx, total, resultCopy)
	return
ERR:
	golog.Errorf("失败。原因：%s，错误：%s", code.String(), err)
	utils.InternalServerError(ctx, code, err)
}

func (c *Job) create(ctx iris.Context) {
	var (
		code utils.Code
		err  error
		vo = new(model.Job)
	)
	if err = ctx.ReadJSON(&vo); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}
	if err = utils.Validator().Struct(vo); err != nil {
		code = utils.CODE_SYS_PARSE_PARAMS_ERROR
		goto ERR
	}

	if err = c.service.Insert(vo); err != nil {
		goto ERR
	}

	utils.Ok_(ctx, utils.CODE_JOB_CREATE_OK)
	return
ERR:
	golog.Errorf("岗位[%s]创建失败。原因：%s，错误：%s", vo.Name, code, err)
	utils.InternalServerError(ctx, code, err)
}

func (c *Job) getAll(ctx iris.Context) {
	var (
		err    error
		code   utils.Code
		total  int64
		result []*model.Job
		resultCopy []*model.JobCopy
	)

	if total, result, err = c.service.FindList(); err != nil {
		goto ERR
	}

	for _, v := range result {
		resultCopy = append(resultCopy, &model.JobCopy{*v})
	}

	utils.PaginationTableData(ctx, total, resultCopy)
	return
ERR:
	golog.Errorf("失败。原因：%s，错误：%s", code.String(), err)
	utils.InternalServerError(ctx, code, err)
}

func (c *Job) get(ctx iris.Context) {
	var (
		code      utils.Code
		err       error
		job *model.Job
		uid       int64
	)
	if uid, err = ctx.URLParamInt64("id"); err != nil {
		goto ERR
	}
	if job, err = c.service.FindOne(&model.Job{ID:uint(uid)}); err != nil {
		code = utils.CODE_SYS_FAILUR
		goto ERR
	}
	utils.Ok(ctx, utils.CODE_OK, &model.JobCopy{*job})
	return
ERR:
	golog.Errorf("~~> 获取岗位[%d]信息失败。原因：%s，错误：%s", uid, code.String(), err.Error())
	utils.InternalServerError(ctx, code, err)
	return
}

func (c *Job) update() {

}

func (c *Job) delete() {

}